﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PayRoller.Code;
using System.Globalization;
using Huayou.Utils;
using HySoft.Cash;
using System.Text;

namespace PayRoller.UControls
{
    public partial class TXTFormatDate : System.Web.UI.UserControl
    {
        /// <summary>
        /// 日期在前台的显示格式
        /// </summary>
        private string formatStringDisplay = "dd/MM/yyyy";
        /// <summary>
        /// 日期在数据库的保存格式
        /// </summary>
        private string formatStringDataBase = "MM/dd/yyyy";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.width > 0)
            {
                this.txtDate.Width = width;
            }

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<script src=\"{0}\" type=\"text/javascript\"></script>",this.ResolveClientUrl( "~/Js/jquery-1.2.6.js"));
            sb.AppendFormat("<script src=\"{0}\" type=\"text/javascript\"></script>", this.ResolveClientUrl("~/Js/date.js"));
            sb.AppendFormat("<script src=\"{0}\" type=\"text/javascript\"></script>", this.ResolveClientUrl("~/Js/jquery.datePicker.min-2.1.2.js"));
            sb.Append("<script language=\"javascript\" type=\"text/javascript\">$(document).ready(function() {        $(\'.datePicker\').datePicker({ \'startDate\': \'"+ StartDate.Day.ToString("00") +"/"+ StartDate.Month.ToString("00") +"/"+ StartDate.Year.ToString("0000") +"\' });    }); </script>");
            
            this.Page.ClientScript.RegisterClientScriptBlock( this.GetType(),"clientScript",sb.ToString(),false);

        }

        private DateTime startDate = new DateTime(2000, 1, 1);
        /// <summary>
        /// 日历表能选择的开始日期
        /// </summary>
        public DateTime StartDate
        {
            get { return this.startDate; }
            set { this.startDate = value; }
        }


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            string tempFormatDB = AppSettings.DateFormatUS;
            if (string.IsNullOrEmpty(tempFormatDB) == false)
            {
                this.formatStringDataBase = tempFormatDB;
            }

            string tempFormatDisplay = AppSettings.DateFormatAU;
            if (string.IsNullOrEmpty(tempFormatDisplay) == false)
            {
                this.formatStringDisplay = tempFormatDisplay;
            }
        }

        private int width = 0;
        /// <summary>
        /// 控件的宽度
        /// </summary>
        public int Width
        {
            get { return this.width; }
            set { this.width = value; }
        }


        private void Display(string dateFormatDB)
        {
            if (string.IsNullOrEmpty(dateFormatDB) == false)
            {
                try
                {
                    DateTime dateConverted = DateUtils.MinDate;
                    DateTime.TryParse(dateFormatDB, AppSettings.DataBaseCulture, DateTimeStyles.None, out dateConverted);
                    this.txtDate.Text = dateConverted.ToString("d", AppSettings.SystemCulture);
                }
                catch
                { }
            }
        }

        private DateTime StoreValue()
        {
            DateTime dateConverted = DateUtils.MinDate;
            try
            {
                string dateString = this.txtDate.Text.Trim();
                if (dateString != string.Empty)
                {

                    DateTime.TryParse(dateString, AppSettings.SystemCulture, DateTimeStyles.None, out dateConverted);
                }
            }
            catch
            {

            }

            return dateConverted;
        }

        private string Store()
        {
            try
            {
                string dateString = this.txtDate.Text.Trim();
                if (dateString != string.Empty)
                {
                    DateTime dateConverted = DateUtils.MinDate;
                    DateTime.TryParse(dateString, AppSettings.SystemCulture, DateTimeStyles.None, out dateConverted);
                    return dateConverted.ToString("d", AppSettings.DataBaseCulture);
                }

                return string.Empty;
            }
            catch
            {
                return string.Empty;
            }
        }

        public string Text
        {
            get
            {
                return Store();
            }
            set
            {
                Display(value);
            }
        }

        public DateTime Value
        {
            get { return this.StoreValue(); }
            //set { }
        }
    }
}